//
//  GQHTabBarController.m
//  GQHKit
//
//  Created by GuanQinghao on 30/09/2017.
//  Copyright © 2017 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark View

#pragma mark Controller
#import "GQHTabBarController.h"
#import "GQHHomeController.h"
#import "GQHServiceController.h"
#import "GQHFreightsController.h"
#import "GQHTrucksController.h"
#import "GQHMeController.h"

#import "GQHLocationManager.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHTabBarController () <UITabBarControllerDelegate>

@end

@implementation GQHTabBarController

#pragma mark --Lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    self.tabBarController.delegate = self;
    
    [self loadTabBarController];
    
    [[GQHLocationManager qh_sharedLocationManager] qh_startUpdatingLocation];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark --Data
- (void)loadLocalData {
    
}

- (void)fetchServiceData {
    
}

#pragma mark --Service


#pragma mark --View
- (void)loadTabBarController {
    
    GQHHomeController *homeController = [[GQHHomeController alloc] init];
    UINavigationController *homeNavController = [[UINavigationController alloc]initWithRootViewController:homeController];
    homeNavController.tabBarItem = [self createTabBarItem:@"首页" imageNamed:@"basic_tabbar_home_unselected" selectedImageNamed:@"basic_tabbar_home_selected"];
    
    GQHServiceController *serviceController = [[GQHServiceController alloc] init];
    UINavigationController *serviceNavController = [[UINavigationController alloc]initWithRootViewController:serviceController];
    serviceNavController.tabBarItem = [self createTabBarItem:@"服务" imageNamed:@"basic_tabbar_service_unselected" selectedImageNamed:@"basic_tabbar_service_selected"];
    
    GQHFreightsController *freightsController = [[GQHFreightsController alloc] init];
    UINavigationController *freightsNavController = [[UINavigationController alloc]initWithRootViewController:freightsController];
    freightsNavController.tabBarItem = [self createTabBarItem:@"找货" imageNamed:@"basic_tabbar_freight_unselected" selectedImageNamed:@"basic_tabbar_freight_selected"];
    
    GQHTrucksController *trucksController = [[GQHTrucksController alloc] init];
    UINavigationController *trucksNavController = [[UINavigationController alloc]initWithRootViewController:trucksController];
    trucksNavController.tabBarItem = [self createTabBarItem:@"找车" imageNamed:@"basic_tabbar_truck_unselected" selectedImageNamed:@"basic_tabbar_truck_selected"];
    
    GQHMeController *meController = [[GQHMeController alloc]init];
    UINavigationController *meNavController = [[UINavigationController alloc]initWithRootViewController:meController];
    meNavController.tabBarItem = [self createTabBarItem:@"我的" imageNamed:@"basic_tabbar_me_unselected" selectedImageNamed:@"basic_tabbar_me_selected"];
    
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {

        [self setViewControllers:@[homeNavController, serviceNavController, freightsNavController, meNavController]];
    } else {

        [self setViewControllers:@[homeNavController, serviceNavController, trucksNavController, meNavController]];
    }
}

- (UITabBarItem *)createTabBarItem:(NSString *)title imageNamed:(NSString *)imageName selectedImageNamed:(NSString *)selectedImageName {
    
    // font
    NSDictionary *unselected = @{NSFontAttributeName:kSmallestFont,NSForegroundColorAttributeName:kDarkGrayColor};
    NSDictionary *selected = @{NSFontAttributeName:kSmallestFont,NSForegroundColorAttributeName:kDarkBlackColor};
    // image
    UIImage *image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // tabBarItem
    UITabBarItem *tabBarItem = [[UITabBarItem alloc]initWithTitle:title image:image selectedImage:selectedImage];
    [tabBarItem setTitleTextAttributes:unselected forState:UIControlStateNormal];
    [tabBarItem setTitleTextAttributes:selected forState:UIControlStateSelected];
//    [tabBarItem setImageInsets:UIEdgeInsetsMake(5, 5, -5, -5)];  // top = -bottem  left = -right
    
    return tabBarItem;
}

@end
